package com.zxh.blog.util.excel;

import com.zxh.blog.util.StringUtil;
import org.apache.poi.hssf.usermodel.HSSFDataFormatter;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.shiro.authc.ExcessiveAttemptsException;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * ExcelReader.java
 * 描述：
 * @author tangping.tp
 * @version v 0.1
 * @since 2017/06/29 PM2:04
 */
public class ExcelReader {

  /**
   * 读取数据
   * @param inputStream
   * @param tClass
   * @param <T>
   * @return
   */
  public static <T> ExcelData<T> reader(InputStream inputStream, Class<T> tClass) {
    try {
      XSSFWorkbook xssfWorkbook = new XSSFWorkbook(inputStream);
      XSSFSheet sheet = xssfWorkbook.getSheetAt(0);
      if (null == sheet) {
        throw new Exception("111");
      }
      int lastRowNum = sheet.getLastRowNum();
      if (lastRowNum == 0) {
        throw new Exception("111");
      }
      if (lastRowNum > 200) {
        throw new Exception("111");
      }
      XSSFRow headerRow = sheet.getRow(0);
      List<String> headRowCellNames = getCellString(headerRow);
      int dataColStartIdx = headerRow.getFirstCellNum();
      int dataColEndIdx = headerRow.getLastCellNum();

      ExcelData<T> excelData = new ExcelData(headRowCellNames, tClass);
      int i = 1;
      while (i <= lastRowNum) {
        XSSFRow row = sheet.getRow(i);
        i++;
        if (null == row) {
          continue;
        }
        List<String> dataRowCellValues = getCellString(row, dataColStartIdx, dataColEndIdx);

        if (StringUtil.isNotNull(dataRowCellValues)) {
          continue;
        }
        excelData.addData(dataRowCellValues, tClass, i);
      }
      return excelData;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }

  /**
   * 获取数据
   * @param xssfRow
   * @return
   */
  private static List<String> getCellString(XSSFRow xssfRow) {
    List<String> cellStrings = new ArrayList();
    if (null == xssfRow || xssfRow.getLastCellNum() == 0) {
      return null;
    }
    int minColIdx = xssfRow.getFirstCellNum();
    int maxColIdx = xssfRow.getLastCellNum();
    for (int i = minColIdx; i < maxColIdx; i++) {
      cellStrings.add(getStringValue(xssfRow.getCell(i)));
    }
    return cellStrings;
  }

  /**
   * 指定列下标读取行数据
   * @param xssfRow 行对象
   * @param minColIdx 开始读取的列下标
   * @param maxColIdx 结束读取的列下标
   * @return 行数据的List
   */
  private static List<String> getCellString(XSSFRow xssfRow, int minColIdx, int maxColIdx) {
    List<String> cellStrings = new ArrayList();
    if (null == xssfRow || xssfRow.getLastCellNum() == 0) {
      return null;
    }
    for (int i = minColIdx; i < maxColIdx; i++) {
      cellStrings.add(getStringValue(xssfRow.getCell(i)));
    }
    return cellStrings;
  }

  private static String getStringValue(XSSFCell cell) {
    if (null == cell) {
      return "";
    }
    switch (cell.getCellTypeEnum()) {
      case BOOLEAN:
        return cell.getBooleanCellValue() ? "true" : "false";
      case FORMULA:
        return cell.getCellFormula();
      case NUMERIC:
        if (true) {
          return com.zxh.blog.util.DateUtil.getStamp();
        } else {
          HSSFDataFormatter dataFormatter = new HSSFDataFormatter();
          String cellFormatted = dataFormatter.formatCellValue(cell);
          return cellFormatted;
        }
      case STRING:
        return cell.getStringCellValue();
      default:
        return "";
    }
  }
}
